package com.webshopping.service.order;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webshopping.entry.Commodity;
import com.webshopping.entry.Image;
import com.webshopping.entry.Order;
import com.webshopping.entry.SubOrder;
import com.webshopping.entry.User;
import com.webshopping.mapper.CommodityMapper;
import com.webshopping.mapper.ImageMapper;
import com.webshopping.mapper.OrderMapper;
import com.webshopping.mapper.ShopCarMapper;
import com.webshopping.mapper.SubOrderMapper;

@Service
public class OrderService {

	@Autowired
	private OrderMapper om;
	@Autowired
	private CommodityMapper cm;
	@Autowired
	private ImageMapper im;
	@Autowired
	private SubOrderMapper soMapper;
	@Autowired
	private ShopCarMapper scMapper;
	
	
	/**
	 * 
	 * @param order
	 * @return
	 */
	public int addOrder(Order order){
		String orderNumber = getOrderNumber();
		order.setOrderNumber(orderNumber);
		int rows1 = om.add(order);
		System.out.println(order);
		int rows2 = 0;
		for(SubOrder s:order.getSubOrders()){
			
			s.setOrderId(order.getId());
			System.out.println(s);
			rows2 = rows2+soMapper.add(s);
			scMapper.delete(Integer.parseInt(s.getShopCarIdList()));
			
		}
		return rows2+rows1;
	}
	
	
	public String getOrderNumber(){
		int id = getOrderNum()+1;
		Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateString = formatter.format(currentTime);
		Random rand = new Random();
        int shu2 = rand.nextInt(9999-1000)+1000;
        String orderNum = id+dateString+shu2;
        //System.out.println("om:"+orderNum);
		return orderNum;
	}
	public int delete(Order order){
		int rows = om.delete(order);
		int rows2 = soMapper.delete(order.getId());
		System.out.println(rows+rows2);
		return rows+rows2;
	}
	
	public int getOrderNum(){
		return om.getOrderNum();
	}
	
	public List<Order> query(Order order){
		return om.query(order);
	}
	
	
	public User findUserAndItemsResultMap(User user,HttpServletRequest request){
		User userRes = om.findUserAndItemsResultMap(user);
		if(userRes!=null){
			for(Order order:userRes.getOrders()){
				for(SubOrder sOrder:order.getSubOrders()){
					
					int commodityId = sOrder.getCommodityId();
					System.out.println(commodityId);
					Commodity c = cm.get(commodityId);
					List<Image> imgs = im.queryImg(commodityId);
					System.out.println(imgs.size());
					if (imgs.size()>0) {
						for (Image img : imgs) {
							if (img.getImg() != null) {
								String imgPath = request.getSession().getServletContext().getRealPath("Imgs");
								File file = new File(imgPath + "/" + img.getImgName());
								if (!file.exists()) {
									InputStream input = new ByteArrayInputStream(img.getImg());
									try {
										FileUtils.copyInputStreamToFile(input, file);
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}
						c.setImgs(imgs);
					}
					
					sOrder.setCommodity(c);
				}
			}
		}
		return userRes;
	}
}
